
var g_pushOver_optionTextArray = new Array(2);
var g_pushOver_optionFuncArray = new Array(2);
var g_pushOver_text;
var g_markedText;

function getSelectionText() {
    var text = "";
    if (window.getSelection) {
        text = window.getSelection().toString();
    } else if (document.selection && document.selection.type != "Control") {
        text = document.selection.createRange().text;
    }
    return text;
}

$(document).ready(function (){
   $('p,li,div,pre').mouseup(function (e){
       var text = getSelectionText();
	   if (text == "") {
			g_markedText = text;
			g_pushOver_text = "---";
			return;
		}
	   //alert(text);
       g_pushOver_optionTextArray = new Array(2);
	   g_pushOver_optionFuncArray = new Array(2);
	   
       g_pushOver_optionTextArray[0] = "add it to knowledge base";
	   g_pushOver_optionTextArray[1] = "translate the german rest";
	   
	   g_pushOver_optionFuncArray[0] = function() { showSentenceSorter() };
	   g_pushOver_optionFuncArray[1] = function() { alert("not implemented yet ...") };
	   
	   var shorty = text;
	   g_markedText = text;
	   if (shorty.length > 30) {
		shorty = shorty.substr(0,12) + "..." + shorty.substr(shorty.length-12,shorty.length-1);
	   }
	   g_pushOver_text = "Handle marked sentence:<br/>" + shorty;	   
	   //clippy.choose_option("What do you want to do with the marked text:\n" + text, optionArray);
   })
   	
});

function LinksWaitForOptionsLoop (agent) {
	if (g_pushOver_text != "") {		
		agent._complete = true;
		if (g_pushOver_text != "---") {
			//alert("found some highlighted text:" + g_pushOver_text + "\nopt1 = "+  g_pushOver_optionTextArray.length + " opt2 = "+  g_pushOver_optionFuncArray.length);
			agent.choose_option(g_pushOver_text, g_pushOver_optionTextArray, g_pushOver_optionFuncArray);
		} else {
			// we have deselected the sentence -> the popup should disappear ...
			//alert("closing the baloon");
			agent.closeBalloon();
		}
		g_pushOver_text = "";			
	} else {
		if (agent._isIdleAnimation()) {
			agent.animate();
		}
	}
	setTimeout(function() {LinksWaitForOptionsLoop(agent)},3500);
}

clippy.load('Links', function(agent) {
	// Do anything with the loaded agent
	agent.show();
	//agent.speak("Hi visitor.\nPlease mark some text on the website so we can start work...");
	agent.speak("Mark my words ;0)");
	/*
	var arrayOfHeadings = [];
	var differentHeadings = [ "h1", "h2", "h3"]; //, "h4", "h5" ];
	for (var l=0; l<differentHeadings.length; l++) {
		//agent.speak("looked for " + differentHeadings[l]);
		var elements = document.getElementsByTagName(differentHeadings[l]);
		for (var i = 0; i < elements.length; i++) {			
			arrayOfHeadings.push(elements[i].innerText);
			//agent.speak("found something - no " + i);
		}
	}
	if (arrayOfHeadings.length > 0) {
		//agent.speak("Look what funny headings I have found :");
	}
	for (var l=0; l<arrayOfHeadings.length; l++) {
		//agent.speak("No " + l + ": " + arrayOfHeadings[l]);
	}
	
	var arrayOfHeadings = [];
	var differentHeadings = [ "p", "li"];
	for (var l=0; l<differentHeadings.length; l++) {
		//agent.speak("looked for " + differentHeadings[l]);
		var elements = document.getElementsByTagName(differentHeadings[l]);
		for (var i = 0; i < elements.length; i++) {			
			var tagContent = elements[i].innerText;
			tagContent.split(".");
			arrayOfHeadings.push(elements[i].innerText);
			//agent.speak("found something - no " + i);
		}
	}
	*/
	g_pushOver_text = "---";
	LinksWaitForOptionsLoop (agent);
	/*
	if (arrayOfHeadings.length > 0) {
		agent.speak("Look what funny headings I have found :");
	}
	for (var l=0; l<arrayOfHeadings.length; l++) {
		agent.speak("No " + l + ": " + arrayOfHeadings[l]);
	}
	*/

	/*
	agent.speak("now, we'll try to do some CORS");
	var xhr = new XMLHttpRequest();
	xhr.open("GET", "http://omepage.no-ip.org/sita/html_out.pl", true);
	xhr.onreadystatechange = function() {
	if (xhr.readyState == 4) {
		// JSON.parse does not evaluate the attacker's scripts.
			agent.speak("found something ....");
			agent.speak(xhr.responseText);
	} else {
		agent.speak("found nothing ....");
	}
	}
	xhr.send();
	*/
	/*
	var elements = document.getElementsByTagName("img");
	for (var i = 0; i < elements.length; i++) {			
		if (elements[i].width < 400) {
			continue;
		}
		if (elements[i].height < 300) {
			continue;
		}
		var nameOfImg = elements[i].alt;
		var srcOfImg = elements[i].src;
		var orgBorder = elements[i].border;
		elements[i].border = 5;
		agent.speak("Found a relatively big image:\n" + nameOfImg + "\n(" + srcOfImg + ")");
		elements[i].border = orgBorder;
	}*/
});
